<!-- src/views/company/CompanyProfileSetup.vue -->
<template>
  <div class="company-profile-setup">
    <div class="setup-container">
      <el-card class="setup-card">
        <template #header>
          <div class="card-header">
            <h2>完善企业信息</h2>
            <p>请填写以下信息以完成企业账户设置</p>
          </div>
        </template>

        <el-form
          :model="profileForm"
          :rules="profileRules"
          ref="profileFormRef"
          label-width="120px"
          class="profile-form"
        >
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="企业名称" prop="name">
                <el-input v-model="profileForm.name" placeholder="请输入企业名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="统一社会信用代码" prop="unifiedCode">
                <el-input v-model="profileForm.unifiedCode" placeholder="请输入统一社会信用代码" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="法人代表" prop="legalPerson">
                <el-input v-model="profileForm.legalPerson" placeholder="请输入法人代表" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="注册资本(元)" prop="registeredCapital">
                <el-input-number
                  v-model="profileForm.registeredCapital"
                  :min="0"
                  controls-position="right"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="成立日期" prop="establishmentDate">
                <el-date-picker
                  v-model="profileForm.establishmentDate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择成立日期"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系人" prop="contactPerson">
                <el-input v-model="profileForm.contactPerson" placeholder="请输入联系人" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="联系电话" prop="contactPhone">
                <el-input v-model="profileForm.contactPhone" placeholder="请输入联系电话" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系邮箱" prop="contactEmail">
                <el-input v-model="profileForm.contactEmail" placeholder="请输入联系邮箱" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="企业网址" prop="website">
                <el-input v-model="profileForm.website" placeholder="请输入企业网址" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="企业Logo" prop="logo">
                <el-input v-model="profileForm.logo" placeholder="请输入Logo URL" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="企业地址" prop="address">
            <el-input v-model="profileForm.address" placeholder="请输入企业地址" />
          </el-form-item>

          <el-form-item label="经营范围" prop="businessScope">
            <el-input
              v-model="profileForm.businessScope"
              type="textarea"
              :rows="3"
              placeholder="请输入经营范围"
            />
          </el-form-item>

          <el-form-item label="企业简介" prop="introduction">
            <el-input
              v-model="profileForm.introduction"
              type="textarea"
              :rows="4"
              placeholder="请输入企业简介"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitProfile" :loading="loading">提交</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useCompanyStore } from '../../store/company'
import { useAuthStore } from '../../store/auth'
import { ElMessage } from 'element-plus'

const router = useRouter()
const companyStore = useCompanyStore()
const authStore = useAuthStore()

const profileFormRef = ref()
const loading = ref(false)

const profileForm = reactive({
  name: '',
  unifiedCode: '',
  legalPerson: '',
  registeredCapital: 0,
  establishmentDate: '',
  businessScope: '',
  address: '',
  contactPerson: '',
  contactPhone: '',
  contactEmail: '',
  website: '',
  introduction: '',
  logo: ''
})

const profileRules = {
  name: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
  unifiedCode: [{ required: true, message: '请输入统一社会信用代码', trigger: 'blur' }],
  legalPerson: [{ required: true, message: '请输入法人代表', trigger: 'blur' }],
  contactPerson: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
  contactPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$|^0\d{2,3}-?\d{7,8}$/, message: '请输入正确的电话号码', trigger: 'blur' }
  ],
  contactEmail: [
    { required: true, message: '请输入联系邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
}

// 提交企业信息
const submitProfile = async () => {
  if (!profileFormRef.value) return

  await profileFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await companyStore.registerCompanyProfile(profileForm)
        ElMessage.success('企业信息完善成功')
        // 跳转到提交审核页面
        router.push('/company/profile-review')
      } catch (error) {
        ElMessage.error(error.message || '企业信息完善失败')
      } finally {
        loading.value = false
      }
    }
  })
}

// 获取当前用户信息并填充到表单中
const fetchCurrentUser = async () => {
  try {
    const user = await authStore.fetchCurrentUser()
    if (user) {
      profileForm.name = user.username
      profileForm.contactPerson = user.username
      profileForm.contactPhone = user.phone || ''
      profileForm.contactEmail = user.email || ''
    }
  } catch (error) {
    ElMessage.error('获取用户信息失败')
  }
}

onMounted(() => {
  fetchCurrentUser()
})
</script>

<style scoped>
.company-profile-setup {
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.setup-container {
  width: 100%;
  max-width: 1000px;
  padding: 20px;
}

.setup-card {
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
  text-align: center;
}

.card-header h2 {
  margin: 0 0 10px 0;
  color: #303133;
}

.card-header p {
  margin: 0;
  color: #606266;
}

.profile-form {
  margin-top: 20px;
}
</style>
